package com.hpin.fansea.base;

import android.app.AlertDialog;
import android.app.Fragment;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.os.Looper;
import android.widget.Toast;

import com.hpin.fansea.ui.LoginActivity;
import com.hpin.fansea.utils.CommonUtils;
import com.hpin.fansea.utils.Constant;

/**
 * Created by Administrator on 2015/7/15.
 */
public class BaseFragment extends Fragment {

    protected MyApplication mApplication;
    protected SharedPreferences sp;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mApplication = (MyApplication) getActivity().getApplication();

        sp = getActivity().getSharedPreferences(Constant.CONFIG, getActivity().MODE_PRIVATE);

    }

    /**
     * 土司
     * @param text
     */
    public void showToast(final String text) {
        if (Thread.currentThread() == Looper.getMainLooper().getThread()) {
            // 如果在主线程中
            showToash(text);

        } else {
            // 在子线程中
            getActivity().runOnUiThread(new Runnable() {
                public void run() {
                    showToash(text);
                }
            });

        }
    }

    private void showToash(final String text) {
        if (!CommonUtils.isNull(text)) {
            Toast.makeText(getActivity(), text, Toast.LENGTH_SHORT).show();
        }
    }


    /**
     * 提示游客登陆
     * @param reminder 提示的内容
     */
    protected void remindLogin(String reminder) {

        //游客
        AlertDialog.Builder mbuilder = new AlertDialog.Builder(getActivity());
        mbuilder.setMessage(reminder).setNegativeButton("取消", null).setPositiveButton("登录", new DialogInterface.OnClickListener() {

            @Override
            public void onClick(DialogInterface dialog, int which) {

                mApplication.clearActivity();

                Intent visitorIntent = new Intent(getActivity(), LoginActivity.class);
                startActivity(visitorIntent);
            }
        }).show();

    }

}
